package org.example.lotterysystem.common.pojo;

import lombok.Data;
import org.example.lotterysystem.common.errorcode.ErrorCode;
import org.example.lotterysystem.common.errorcode.GlobalErrorCode;
import org.springframework.util.Assert;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {
    private Integer code;//返回的状态码
    private String message;//状态码描述
    private T data;//返回数据

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = GlobalErrorCode.SUCCESS.getCode();
        result.message = GlobalErrorCode.SUCCESS.getMessage();
        result.data = data;
        return result;
    }

    public static <T> CommonResult<T> error(Integer code, String message) {
        Assert.isTrue(!GlobalErrorCode.SUCCESS.getCode().equals(code), "不是错误的异常");
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.message = message;
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage());
    }
}
